package org.tlipoca.smarticle.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.genntii.smdomain.entity.Article;

import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.ArrayList;

@Mapper
public interface ArticleMapper extends BaseMapper<Article> {

    @Update("update Article set status = #{status} where id = #{articleId};")
    void updateArticleStatus(Long articleId, Integer status);

    ArrayList<Article> getArticleListByIds(@Param("ids") ArrayList<Long> ids);

    void insertGetId(Article article);

    @Select("select * from Article where id = #{id}")
    Article selectArticle(@Param("id") Long id);

    @Update("update Article set `like` = #{like}, dislike = #{dislike} where id = #{articleId}")
    void ArticleLikeUpdate(@Param("like") int like, @Param("dislike")int dislike,@Param("articleId")Long articleId);

    ArrayList<Article> getLatestArticle(@Param("pageNum") int pageNum,
                                        @Param("pageSize") int pageSize,
                                        @Param("order") int order,
                                        @Param("filter") int filter,
                                        @Param("ids") ArrayList<Long> ids);

    long getLatestArticleTotal(@Param("ids") ArrayList<Long> ids,
                               @Param("filter") int filter);

    @Select("select * from Article where user_id = #{userId}")
    ArrayList<Article> selectArticleByUserId(@Param("userId") Long userId);

    @Select("""
            SELECT\s
                a.id,
                a.title,
                a.create_time,
                COUNT(al.like) AS likes_count
            FROM\s
                Article a
            LEFT JOIN\s
                Article_Like.Article_Like al ON a.id = al.article_id
            WHERE\s
                a.channel_id = #{channelId}
                AND al.like = 1 -- 只计算点赞
            GROUP BY\s
                a.id, a.create_time
            ORDER BY\s
                likes_count DESC, a.create_time DESC
            LIMIT #{pageNum},#{pageSize};""")
    ArrayList<Article> getArticleByChannel(@Param("channelId")Long channelId,
            @Param("pageNum")int pageNum,@Param("pageSize")int pageSize);


    @Select("select count(*) from Article where user_id = #{userId}")
    Integer getUserArticleCount(@Param("userId") Long userId);

    @Update("update Article set view = view+1 where id = #{id};")
    void viewCountPlus1(@Param("id") Long ArticleId);

    @Select("select * from Article " +
            "where " +
            "channel_id = #{channelId} and title like concat('%', #{keyword},'%')" +
            " and update_time < #{latestUpdateTime} " +
            "order by update_time desc" +
            " limit #{pageNum}, #{pageSize};")
    ArrayList<Article> getArticleListNew(@Param("channelId") Long channelId,
                                         @Param("keyword")String keyword,
                                         @Param("latestUpdateTime")LocalDateTime latestUpdateTime,
                                         @Param("pageNum") int pageNum,
                                         @Param("pageSize")int pageSize);

    @Select("select * from Article " +
            "where " +
            "channel_id = #{channelId} and title like concat('%', #{keyword},'%')" +
            " and update_time < #{latestUpdateTime} " +
            "order by `like`-dislike*1.5 desc" +
            " limit #{pageNum}, #{pageSize};")
    ArrayList<Article> getArticleListHot(@Param("channelId") Long channelId,
                                         @Param("keyword")String keyword,
                                         @Param("latestUpdateTime")LocalDateTime latestUpdateTime,
                                         @Param("pageNum") int pageNum,
                                         @Param("pageSize")int pageSize);

    @Update("update Article set user_follower_count = #{userFollowerCount} where user_id = #{userId}")
    void userFollowerCountUpdate(@Param("userFollowerCount")Integer followerCount,@Param("userId")Long userId);

    @Select("select * from Article " +
            "where user_id = #{userId} " +
            "order by update_time DESC " +
            "limit #{pageNum}, #{pageSize}")
    ArrayList<Article> getArticleLimit(@Param("userId") Long userId,
                                       @Param("pageNum") Integer pageNum,
                                       @Param("pageSize") Integer pageSize);


    @Select("select count(*) from Article where user_id = #{userId};")
    Long getArticleTotalByUser(@Param("userId") Long userId);

    @Update("update Article set `like` = #{like}, dislike = #{dislike} where id = #{articleId}")
    void setLike(@Param("like") Integer like,
                 @Param("dislike") Integer dislike,
                 @Param("articleId") Long articleId);
}
